#include "auth.h"

Any Auth::run()
{
    while (true)
    {
        RequestMessage reqMsg = msgrout2authMQ.pop();

        if ("GET" == reqMsg.req->method)
        {
        }
        else if ("POST" == reqMsg.req->method)
        {
            switch (reqMsg.type)
            {
            case ReqType::REGISTER:
                /*拿出注册信息*/
                Json::Reader reader;
                Json::Value root;
                if (reader.parse(reqMsg.req->body, root))
                {
                    if (0 == db.registerUser(root["username"].asString(),
                                             root["username"].asString(),
                                             "",
                                             "",
                                             "",
                                             "",
                                             "other",
                                             "",
                                             root["email"].asString(),
                                             root["password"].asString()))
                    {
                        reqMsg.res->status = 200;
                        std::cout << "SUCCESS" << std::endl;
                    }
                    else
                    {
                        /*返回异常*/
                        reqMsg.res->status = 524;
                    }
                }
                else
                {
                    /*返回异常*/
                    reqMsg.res->status = 525;
                }
                break;
            }
        }

        // 通知路由线程该请求处理已完成
        reqMsg.completion_promise->set_value();
    }
}